package com.google.code.jgib.util.irc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.jibble.pircbot.PircBot;
import org.jibble.pircbot.User;

public class IrcBot extends PircBot{

	private ArrayList<Request> reqs = new ArrayList<Request>();

	public IrcBot(String name){
		this.setName(name);
		this.setLogin("JAb");
	}


	public void onMessage(String channel, String sender,
			String login, String hostname, String message) {
		if (message.equalsIgnoreCase("!list")) {
			//			String cmd = "cat /jail/glftpd/ftp-data/logs/glftpd.log | grep NEWDIR";
			String[] cmd = new String[]{"bash","-c","ls /jail/glftpd/site/"};
			//String cmd = "tail --bytes=1024 /jail/glftpd/ftp-data/logs/glftpd.log | grep NEWDIR";
			Runtime run = Runtime.getRuntime();
			Process pr = null;
			try {
				pr = run.exec(cmd);
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				pr.waitFor();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
			String line = "";
			try {
				while ((line=buf.readLine())!=null) {
					this.sendMessage(channel, line);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(message.startsWith("!search ") || message.startsWith("!s ")){
			String[] cmd = new String[]{"bash","-c","ls /jail/glftpd/site/ | grep -i "+message.substring(message.indexOf(' ')+1)};
			Runtime run = Runtime.getRuntime();
			Process pr = null;
			try {
				pr = run.exec(cmd);
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				pr.waitFor();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
			String line = "";
			try {
				while ((line=buf.readLine())!=null) {
					this.sendMessage(channel, line);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(message.startsWith("!voiceme")){
			this.voice(channel, sender);
		}
		if(message.startsWith("!opme")){
			if(sender.equalsIgnoreCase("Warg") || sender.equalsIgnoreCase("Sheepie"))
				this.op(channel, sender);
		}
		
		
		
		
		if(message.startsWith("!f ") || message.startsWith("!fill ")){
			for(Request r : reqs){
				if(r.actualRequest.equals(message.substring(message.indexOf(' ')+1))){
					r.fillReq(sender);
					
					String[] cmd = new String[]{"bash","-c","mv /jail/glftpd/site/\\[REQ\\]-"+message.substring(message.indexOf(' ')+1)+" /jail/glftpd/site/"+message.substring(message.indexOf(' ')+1)};
					Runtime run = Runtime.getRuntime();
					Process pr = null;
					try {
						pr = run.exec(cmd);
					} catch (IOException e) {
						e.printStackTrace();
					}
					try {
						pr.waitFor();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					this.sendMessage(sender, "You have successfully filled request '"+message.substring(message.indexOf(' ')+1)+"'.");
					User[] users = this.getUsers(channel);
					for(User u : users){
						if(u.getNick().contains(r.requester)){
							this.sendMessage(u.getNick(), "Your request ('"+message.substring(message.indexOf(' ')+1)+"') has been filled by "+sender+".");
							reqs.remove(r);

						}
					}
				}
			}
		}
	}
	
	@Override
	protected void onJoin(String channel, String sender, String login, String hostname) {
		for(Request r : reqs){
			if(r.isFilled()){
				User[] users = this.getUsers(channel);
				for(User u : users){
					if(u.getNick().contains(r.requester)){
						this.sendMessage(u.getNick(), "Your request ('"+r.actualRequest+"') has been filled by "+r.filler+".");
						reqs.remove(r);
					}
				}
			}
		}
	}

	@Override
	protected void onPrivateMessage(String sender, String login, String hostname, String message){
		if(message.startsWith("!r ") || message.startsWith("!request ")){
			String[] cmd = new String[]{"bash","-c","mkdir /jail/glftpd/site/\\[REQ\\]-"+message.substring(message.indexOf(' ')+1).replace(' ', '.').replace('/', '.').replace('\\', '.')};
			Runtime run = Runtime.getRuntime();
			Process pr = null;
			try {
				pr = run.exec(cmd);
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				pr.waitFor();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			this.sendMessage(sender, "Your request ('"+message.substring(message.indexOf(' ')+1)+"') has been posted.");
			Request newR = new Request();
			newR.requester=sender;
			newR.actualRequest=message.substring(message.indexOf(' ')+1);
			reqs.add(newR);
		}


	}

}